package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.damain.Dish;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class DishController {
    @Autowired
    private DishService dishService;
    //分页查询
    @GetMapping("/dish/page")
    public ResultInfo findall(@RequestParam(value = "page",defaultValue = "1") Integer pagenum,
                              @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize,String name){
        Page page=dishService.findByPage(pagenum,pageSize,name);
        return ResultInfo.success(page);
    }
    //新增
    @PostMapping("/dish")
    public ResultInfo save(@RequestBody Dish dish){
        dishService.save(dish);
        return ResultInfo.success(null);
    }
    //查询菜品信息
    @GetMapping("/dish/{id}")
    public ResultInfo findByid(@PathVariable("id") Long id){
        Dish dish=dishService.findByid(id);
        return ResultInfo.success(dish);
    }
    //修改
    @PutMapping("/dish")
    public ResultInfo update(@RequestBody Dish dish){
        dishService.update(dish);
        return ResultInfo.success(dish);
    }
    //删除
    @DeleteMapping("/dish")//@RequestParam保证接受的参数相同
    public ResultInfo delete(@RequestParam("ids")List<Long>id){
        dishService.deleteByIds(id);
        return ResultInfo.success(null);
    }
    //停售-起售
    @PostMapping("/dish/status/{status}")
    public ResultInfo status(@PathVariable("status") Integer status,
                             @RequestParam("ids") List<Long>id){
        dishService.status(status,id);
        return ResultInfo.success(null);
    }
    
}
